<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
*{
	font-size:1dp;
}
</style>
<div style='height:auto'>
	<div style='display:table;width:100%'>
		<div style='width:50%;display:table-cell'>
			持仓:<br><input type='text' style='border:0px' id='cm' value=0>
		</div>
		<div style='width:50%;display:table-cell'>
			金额:<br><input type='text' style='border:0px' id='mn' value=0>
		</div>
	</div>
	<div>
		<span>总价<span id='zj'></span></span>&nbsp;
	</div>
	<div>
		<span>价格&nbsp;数量
		<textarea style='width:100%;border:0px;height:200px;display:block' id='line'></textarea>
	</div> 
	<button id='enter'>确定</button>
</div>
<script>
	var dayOfTimes = 15 , dayOfOpt = dayOfTimes , startAmount = 0 ,
		curOfTimes = 0 , curOfDay = 0 , curAmount = 0 , rate = 0;
	function init(){

	}
	function resetAmount( p ){
		curAmount = (p * parseInt(id('cm').value * 100 ) + parseFloat(id('mn').value ) ).toFixed(2) ;
		id('zj').innerHTML = curAmount ;
	}
	function id( id ){
		return document.getElementById( id ) ;
	}
	function clear(){
		var lines = id( 'line' ).value.split('\n') , newLines = ''  ;
		for( var key in lines ){
			if( !/^([0-9]+(\.[0-9]{1,2})?)\s*([+-]?[0-9]+)?$/.test( lines[key] ) ){
				continue ;
			}
			newLines += lines[key] + '\n' ;
		}
		id( 'line' ).value = newLines ;
	}
	function caculate(){
		//clear() ;
		var lines = id( 'line' ).value.split('\n') ;
		if( !lines.length ) return ;
		var ptn = /^([0-9]+(\.[0-9]{1,2})?)\s+([+-]?[0-9]+)$/.exec( lines[lines.length-1]) ;
			var p , h , th , tp , isNext = true ;
		if( ptn ) {
			p = parseFloat(ptn[1]) ; 
			h = parseInt(ptn[3]) ;
			th = parseInt( id('cm').value ) ; 
			tp = parseFloat( id('mn') .value ) ;
			th += h;
			tp += p * h * 100 * -1 ;
			if( th < 0 || tp < 0) isNext = false ;
			else {
				id('cm').value = th ;
				id('mn').value = tp.toFixed(2) ;
			}
		}
		if( isNext ) next() ;
	}

	function next(){
		var lines = id( 'line' ).value.split('\n') ;
		var ptn = /^([0-9]+(\.[0-9]{1,2})?)\s*([+-]?[0-9]+)?$/.exec( lines[lines.length-1]) ;
		var p = parseFloat(ptn[1]) ;
		
		p += getOffset() ;
		if( lines.length >= 15 ) id( 'line' ).value = '' ;
		id('line').value += '\n'+p.toFixed(2)+' ' ;
		resetAmount( p ) ;
	}

	function getOffset(){
		var d = 0.01;
		if( Math.random() >= 0.5 ) d *= -1 ;
		console.log((d * getOpen()).toFixed(2))
		return parseFloat((d * getOpen()).toFixed(2)) ;
	}

	function getOpen(){
		dayOfOpt -- ;
		var offset = 1 ;
		if( dayOfOpt > 0 ) 
			return offset ;
		else 
			offset = Math.round( Math.random() * 3 + 1 ) ;
		dayOfOpt = dayOfTimes ;
		return Math.random() >= 0.5 ? offset *= -1 : offset ;
	}

	id('line').onkeydown = function( e ){
		if( e.keyCode == 13 ){
			caculate() ;
			return false ;
		}
	}

	id('enter').onclick=function(){
		caculate() ;
	}

</script>